1559B - Mocha and Red and Blue - CodeForces Solution


dp greedy *900

Please click on ads to support us..

Python Code:

def find_first_letter(input_str):
    for i in range(len(input_str)):
        if input_str[i] == 'R':
            return 'R' if i & 1 == 0 else 'B'
        if input_str[i] == 'B':
            return 'B' if i & 1 == 0 else 'R'
 
    return 'B'
 
t = int(input())
 
for iter in range(t):
    n = int(input())
    input_str = input()
    result_str = ''
    if input_str[0] == '?':
        if n > 1:
            result_str += find_first_letter(input_str)
            input_str = result_str + input_str[1:]
        else:
            print('B')
            continue
    else:
        result_str += input_str[0]
 
    for i in range(1, n):
        if input_str[i] in ['R', 'B']:
            result_str += input_str[i]
            continue
 
        if result_str[i - 1] == 'B':
            result_str += 'R'
        elif result_str[i - 1] == 'R':
            result_str += 'B'
 
    print(result_str)
 

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define py cout<<"YES"<<endl
#define pn cout<<"NO"<<endl
#define e cout<<"=\n"
#define g cout<<">\n"
#define l cout<<"<\n"
#define Pans cout<<"ans=="<<" ";
bool is_lleger(float k) {return floor(k) == k;}

int main()
{
    ll t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        string s;
        cin>>s;
        int i=0;
        while(s[i]=='?'){
            i++;
        }

        if(i==n){
            char c='B';
            for(int i=0;i<n;i++){
                s[i]=c;
                if(c=='R')c='B';
                else c='R';
            }
        }
        else{
            for(int j=i+1;j<n;j++){
                if(s[j]=='?'){
                    if(s[j-1]=='R') s[j]='B';
                    else if(s[j-1]=='B') s[j]='R';
                }
            }
            for(i=i-1;i>=0;i--){
                if(s[i]=='?'){
                    if(s[i+1]=='R') s[i]='B';
                    else if(s[i+1]=='B') s[i]='R';
                }
            }
        }

      //   Pans;
        cout<<s<<endl;
    }
return 0;
}


Comments

Submit
0 Comments
More Questions

461A - Appleman and Toastman
320B - Ping-Pong (Easy Version)
948A - Protect Sheep
387A - George and Sleep
53A - Autocomplete
1729G - Cut Substrings
805B - 3-palindrome
805C - Find Amir
676C - Vasya and String
1042B - Vitamins
1729F - Kirei and the Linear Function
25D - Roads not only in Berland
1694A - Creep
659F - Polycarp and Hay
1040A - Palindrome Dance
372A - Counting Kangaroos is Fun
1396B - Stoned Game
16A - Flag
1056A - Determine Line
670B - Game of Robots
1418C - Mortal Kombat Tower
1382B - Sequential Nim
1272C - Yet Another Broken Keyboard
808A - Lucky Year
1245A - Good ol' Numbers Coloring
58B - Coins
1041C - Coffee Break
507A - Amr and Music
1041D - Glider
1486A - Shifting Stacks